package com.mida.dms.knw.Result;

import com.mida.dms.knw.entity.DtOid;
import com.mida.dms.knw.mapper.DtOidMapper;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import java.util.Collections;

/**
 * 前端请求返回结果处理
 */
public abstract class BaseResult {

    public static final Long PUBLISH_STATUS = 145L;

    public static final Integer DT_OID = 0;

    public static final Integer KNW_OID = 1;

    /**
     * OID
     */
    @Autowired
    private DtOidMapper dtOidMapper;


    protected void saveOid(Long soid, String oid) throws Exception {
        DtOid dtoid = new DtOid(oid, soid);
        dtOidMapper.insert(dtoid);
    }


    public static void buildListSuccess(ListResult result, Object data) {
        try {
            result.setData(data);
            result.setStatus(HttpStatus.OK.value());
            result.setMessage("success");
        } catch (Exception var2) {
            buildListWarn(result, var2);
        }
    }

    public static void buildListSuccess(ListResult result) {

        try {
            result.setData(Collections.emptyMap());
            result.setStatus(HttpStatus.OK.value());
            result.setMessage("success");
        } catch (Exception var2) {
            buildListWarn(result, var2);
        }
    }

    public static void buildListWarn(ListResult result, Exception e) {
        result.setData(Collections.emptyMap());
        result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(e.getMessage());
    }

    public static void buildListWarn(ListResult result, String message) {
        result.setData(Collections.emptyMap());
        result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(message);
    }

    public static void buildBadRequestListWarn(ListResult result, String message){
        result.setData(Collections.emptyMap());
        result.setStatus(HttpStatus.BAD_REQUEST.value());
        result.setMessage(message);
    }

    public static void buildActionSuccess(ActionResult actionResult) {
        try {
            actionResult.setStatus(HttpStatus.OK.value());
            actionResult.setMessage("success");
        } catch (Exception var1) {
            buildActionWarn(actionResult, var1);
        }
    }

    public static void buildActionSuccess(ActionResult actionResult, String message) {
        try {
            if (StringUtils.isEmpty(message)) {
                actionResult.setStatus(HttpStatus.OK.value());
                actionResult.setMessage(" ");
            }
            actionResult.setStatus(HttpStatus.OK.value());
            actionResult.setMessage(message);
        } catch (Exception var1) {
            buildActionWarn(actionResult, var1);
        }
    }

    public static void buildActionWarn(ActionResult result, Exception e) {
        result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(e.getMessage());
    }

    public static void buildActionWarn(ActionResult result, String message) {
        result.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(message);
    }

    public static void buildBadRequestActionWarn(ActionResult result, String message) {
        result.setStatus(HttpStatus.BAD_REQUEST.value());
        result.setMessage(message);
    }

    public static ActionResult buildActionSuccess() {
        ActionResult actionResult = new ActionResult();
        try {
            actionResult.setStatus(HttpStatus.OK.value());
            actionResult.setMessage("success");
        } catch (Exception var1) {
            buildActionWarn(actionResult, var1);
        }

        return actionResult;
    }
}
